<template>
  <div class="test-component">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>测试 minValue 函数</span>
        </div>
      </template>
      
      <div class="test-section">
        <h3>输入数组</h3>
        <div class="input-area">
          <el-input
            v-model="inputString"
            placeholder="请输入数字，用逗号分隔（例如：1,2,3）"
            @change="updateResult"
          ></el-input>
        </div>
        
        <h3>计算结果</h3>
        <div class="result-area">
          <el-alert
            v-if="error"
            :title="error"
            type="error"
            :closable="false"
          ></el-alert>
          <el-alert
            v-else
            :title="`最小值是: ${result}`"
            type="success"
            :closable="false"
          ></el-alert>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { minValue } from '../../../src/utils/minValue'

const inputString = ref('5,3,9,1,7')
const result = ref(null)
const error = ref(null)

const updateResult = () => {
  try {
    error.value = null
    // 将输入字符串转换为数字数组
    const numbers = inputString.value
      .split(',')
      .map(num => num.trim())
      .filter(num => num !== '')
      .map(num => Number(num))
    
    // 检查是否有非数字值
    if (numbers.some(isNaN)) {
      throw new Error('输入包含非数字值')
    }
    
    // 使用minValue函数计算最小值
    result.value = minValue(numbers)
  } catch (err) {
    error.value = err.message
    result.value = null
  }
}

// 初始化时计算一次结果
onMounted(() => {
  updateResult()
})
</script>

<style>
.test-component {
  margin: 20px 0;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: bold;
}

.test-section {
  margin-top: 10px;
}

.input-area, .result-area {
  margin: 10px 0 20px 0;
}

h3 {
  font-size: 16px;
  margin-bottom: 10px;
  color: #606266;
}
</style>